<template>
  <section>
     <!-- 面包屑 -->
    <breadcrumbs></breadcrumbs>
    <!-- 查询条件 -->
    <el-form ref="searchForm"
             :model="searchForm"
             label-width="100px"
             inline
             class="demo-form-inline"
             size='small'
             style="border-bottom: 1px solid #f6f6f6;">
      <el-form-item label="菜单名称"
                    prop="name">
        <el-input v-model="searchForm.name"
                  placeholder="请输入菜单名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="search">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <btns @exec="exec"></btns>
    <div class="data-container scroll-table">
      <div>
        <div class="ind-title lh50 f16">菜单列表</div>
      </div>
      <el-table :data="tableData"
                style="width: 100%"
                row-key="menuId"
                border
                lazy
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                @selection-change="tableSelectionChange">
        <el-table-column type="selection"
                         width="40">
        </el-table-column>
        <el-table-column prop="menuName"
                         label="名称">
        </el-table-column>
        <el-table-column prop="menuIcon"
                         label="图标">
        </el-table-column>
        <el-table-column prop="url"
                         label="路径">
        </el-table-column>
        <el-table-column prop="sort"
                         label="排序">
        </el-table-column>
        <el-table-column prop="status"
                         label="状态">
          <template slot-scope="scope">
            <div v-if="scope.row.status==0"
                     type="danger"
                     :underline="false">禁用</div>
            <div v-if="scope.row.status==1"
                     :underline="false">启用</div>
          </template>
        </el-table-column>
        <el-table-column fixed="right"
                         label="操作"
                         width="100">
          <template slot-scope="scope">
            <div :underline="false"
                     type="primary"
                     v-if="scope.row.pid==0"
                     @click="addnext(scope.row)">新增下级</div>&nbsp;
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--新增修改界面-->
    <el-dialog :title="(funTit=='add'?'新增':'修改')+(pForm?'下级-'+pForm.menuName:'')"
               :visible.sync="addORUpdateVisible"
               width="700px"
               :close-on-click-modal="false">
      <el-form :model="menuForm"
               label-width="80px"
               :rules="rules"
               ref="menuForm">
        <el-form-item label="图标"
                      prop="menuIcon">
          <el-input v-model="menuForm.menuIcon"
                    auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单名称"
                      prop="menuName">
          <el-input v-model="menuForm.menuName"
                    auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="url"
                      prop="url">
          <el-input v-model="menuForm.url"
                    auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序"
                      prop="sort">
          <el-input v-model="menuForm.sort"
                    auto-complete="off"
                    @input="menuForm.sort=menuForm.sort.replace(/[^\d^\.]+/g,'')"
                    type="text"></el-input>
        </el-form-item>
        <el-form-item label="绑定按钮"
                      prop="btnIds"
                      class="mytransfer">
          <el-transfer :titles="['请选择', '已选择']"
                       filterable
                       :filter-method="filterMethod"
                       filter-placeholder="请输入按钮名称"
                       :props="{key: 'btnId',label: 'btnName'}"
                       v-model="menuForm.btnIds"
                       :data="btnData"></el-transfer>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click.native="addORUpdateVisible = false">取消</el-button>
        <el-button type="primary"
                   @click.native="submit('menuForm')">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import btns from '@/components/btns.vue';
import breadcrumbs from "@/components/breadcrumbs.vue";
export default {
  components: {
    btns,
    breadcrumbs,
  },
  data () {
    return {
      searchForm: {
        name: ''
      },
      btnData: [],
      filterMethod (query, item) {
        return item.btnName.indexOf(query) > -1;
      },
      data: [],
      tableData: [],
      tableSelection: [],
      addORUpdateVisible: false,
      funTit: '',
      pForm: null,
      menuForm: {
        "btnIds": [],
        "menuIcon": "",
        "menuName": "",
        "pid": 0,
        "sort": 0,
        "url": ""
      },
      rules: {
        "menuIcon": [],
        "menuName": [
          { required: true, message: '请输入菜单名称', trigger: 'blur' }
        ],
        "url": [],
        "sort": [
          { required: true, message: '排序', trigger: 'blur' }
        ]
      },
    }
  },
  created () {
    this.menulist();
    this.btnlist();
  },
  methods: {
    btnlist () {
      //获取接口
      this.$api.system.btnlist({}).then(d => {
        if (d.code == 200) {
          d.data.forEach(e => {
            e.btnId = e.btnId + '';
          })
          this.btnData = d.data;
        } else {
          this.$message.error(d.msg);
        }
      }).catch((e) => {
        console.log(e);
      })
    },
    handleReset () {
      this.$refs.searchForm.resetFields();
      this.search()
    },
    search () {
      if (this.searchForm.name) {
        var data = this.data.filter(e => e.menuName.indexOf(this.searchForm.name) >= 0 && e.pid != 0 || e.pid == 0);
        this.getdata(data, true);
      } else {
        var data = this.data
        this.getdata(data, true);
      }
    },
    menulist () {
      //获取接口
      this.$api.system.menulist({}).then(d => {
        if (d.code == 200) {
          this.data = d.data;
          this.getdata(this.data);
        } else {
          this.$message.error(d.msg);
        }
      }).catch((e) => {
        console.log(e);
      })
    },
    getdata (nowdata, isde) {
      var data = nowdata.filter(e => e.pid == 0);
      var temp = [];
      data.forEach(e => {
        e.children = nowdata.filter(c => c.pid == e.menuId);
        if (e.children && e.children.length > 0) {
          e.children.forEach(c => {
            if (!isde)
              c.btnIds = c.btnIds ? c.btnIds.split(',') : [];
          })
        }
        if (!isde)
          e.btnIds = e.btnIds ? e.btnIds.split(',') : [];
        temp.push(e)
      })
      // console.log(temp);
      this.tableData = temp;
    },
    tableSelectionChange (val) {
      this.tableSelection = val;
    },
    exec (fun) {
      if (this[fun]) {
        this.funTit = fun;
        this[fun]()
      } else {
        this.$message.error('方法未定义');
      }
    },
    add () {
      this.pForm = null
      this.addORUpdateVisible = true;
      this.menuForm = {
        "btnIds": [],
        "menuIcon": "",
        "menuName": "",
        "pid": 0,
        "sort": 0,
        "url": ""
      }
    },
    addnext (p) {
      this.funTit = 'add';
      this.pForm = p;
      this.addORUpdateVisible = true;
      this.menuForm = {
        "btnIds": [],
        "menuIcon": "",
        "menuName": "",
        "pid": p.menuId,
        "sort": 0,
        "url": ""
      }
    },
    update () {
      this.pForm = null
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      this.menuForm = this.tableSelection[0];
      this.addORUpdateVisible = true;
    },
    del () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0]
      if (demo.children && demo.children.length > 0) {
        this.$message.error('还有子级菜单不能删除');
        return
      }
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.system.menudelete({ menuId: demo.menuId }).then(d => {
          if (d.code == 200) {
            this.$message.success('删除成功');
            this.menulist();
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
    refresh () {
      this.menulist();
    },
    enable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      if (demo.status == 1) {
        this.$message.error('该数据已启用');
        return
      }
      this.$confirm('此操作将启用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.system.menuupdateStatus({ menuId: demo.menuId, status: 1 }).then(d => {
          if (d.code == 200) {
            this.$message.success('启用成功');
            this.menulist();
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
    disable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      if (demo.status == 0) {
        this.$message.error('该数据已禁用');
        return
      }
      this.$confirm('此操作将禁用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.system.menuupdateStatus({ menuId: demo.menuId, status: 0 }).then(d => {
          if (d.code == 200) {
            this.$message.success('禁用成功');
            this.menulist();
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
    submit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // if (this.data.findIndex(c => c.url == this.menuForm.url) >= 0) {
          //     this.$message.error('code不能重复');
          //     return
          // }
          var data = JSON.parse(JSON.stringify(this.menuForm));
          var btns = data.btnIds.sort();
          data.btnIds = btns.join(',');
          if (this.funTit == 'add') {
            this.$api.system.menuadd(data).then(d => {
              if (d.code == 200) {
                this.$message.success('新增成功');
                this.addORUpdateVisible = false;
                this.$refs.menuForm.resetFields();
                this.menulist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          } else {
            data.children = [];
            this.$api.system.menuupdate(data).then(d => {
              if (d.code == 200) {
                this.$message.success('修改成功');
                this.addORUpdateVisible = false;
                this.$refs.menuForm.resetFields();
                this.menulist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}

</script>

<style>
.mytransfer .el-checkbox {
  display: block;
}
</style>
